//
//  MenuViewController.m
//  Food
//
//  Created by dlios on 15-7-15.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "MenuViewController.h"
#import "BaseHeader.h"
#import "AFNet.h"
#import "MenuObject.h"

#import "MenuListTableViewCell.h"
#import "MenuNextViewController.h"

#import "MenuCellCollectionViewCell.h"
#import "MenuHeaderCollectionViewCell.h"
#import "MBProgressHUD.h"

@interface MenuViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, retain) NSMutableArray *menuArray; // 菜谱主要分类
@property (nonatomic, retain) NSMutableArray *tagsArray; // 具体分类（collectionView的section数组）
@property (nonatomic, retain) NSMutableArray *dataArray; // 具体分类（collectionView的cell数组）
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation MenuViewController
- (void)dealloc
{
    [_flowLayout release];
    [_menuArray release];
    [_tagsArray release];
    [_dataArray release];
    [_tableView release];
    [_collectionView release];
    [_HUD release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"菜谱";
    //self.navigationController.navigationBar.translucent = NO;
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];

    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    myImageView.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:myImageView];
    [myImageView release];
    
    // 初始化
    self.menuArray = [NSMutableArray array];
    self.tagsArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    
    // 函数调用
    [self collectionViewSetup];
    [self tableViewSetUp];
    [self dataHander];
}

// tableView
- (void)tableViewSetUp
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 70, Height - 64 - 44)];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[MenuListTableViewCell class] forCellReuseIdentifier:@"caipu"];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
}

// collectionView
- (void)collectionViewSetup
{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(60, 30);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [_flowLayout release];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(80, 0, Width - 80, Height - 110) collectionViewLayout:self.flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView setShowsVerticalScrollIndicator:NO];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    //菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.removeFromSuperViewOnHide = YES;
    self.HUD.labelText = @"稍等";
    self.HUD.detailsLabelText = @"正在加载中";
    self.HUD.square = YES;
    [self.view addSubview:self.HUD];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    
    //collectionView注册
    [self.collectionView registerClass:[MenuCellCollectionViewCell class] forCellWithReuseIdentifier:@"name"];
    
    //collectionView的header
    self.flowLayout.headerReferenceSize = CGSizeMake(80, 25);
    [self.collectionView registerClass:[MenuHeaderCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

// 数据处理
- (void)dataHander
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败! 请检查网络吧" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        [self.HUD hide:YES];
    }
    [AFNet connectNetGetDataWithURLString:CAIPUURL parameters:nil finishblock:^(id result) {
        
        [self.HUD show:YES];
        self.menuArray = [[result objectForKey:@"data"] objectForKey:@"caipu"];
        [self.tableView reloadData];
        [self.collectionView reloadData];
        [self.HUD hide:YES];
        [self tableView:self.tableView didSelectRowAtIndexPath:0];
        
        
    }];
    
}

// tableView要实现的协议方法1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.menuArray.count;
}

// tableView要实现的协议方法2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    MenuListTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"caipu"];
    tableViewCell.menuLabel.text = [[self.menuArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    tableViewCell.backgroundColor = [UIColor clearColor];
    return tableViewCell;
}

// 点击tableViewcell要执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    //点击方法
    self.tagsArray = [[self.menuArray objectAtIndex:indexPath.row] objectForKey:@"tags"];
    
    [self.collectionView reloadData];
}

// collectionView要实现的协议方法1
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.tagsArray.count == 0) {
        self.tagsArray = [[self.menuArray objectAtIndex:0] objectForKey:@"tags"];
    }
    
    self.dataArray = [[self.tagsArray objectAtIndex:section] objectForKey:@"data"];
    
    return self.dataArray.count;
}

// collectionView要实现的协议方法2
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuCellCollectionViewCell *ectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"name" forIndexPath:indexPath];
    ectionViewCell.backgroundColor = [UIColor clearColor];
    ectionViewCell.clasifyLabel.text = [[[[self.tagsArray objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item] objectForKey:@"name"];
    return ectionViewCell;
}

// 返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.tagsArray.count;
}

// 给header赋值
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 3.和返回cell的协议方法类似, 每当collectionView需要显示一个header时,就会调用这个方法获得一个reuseView
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //  从重用池中获取一个重用的view
        MenuHeaderCollectionViewCell *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        reuseView.nameLabel.text = [NSString stringWithFormat:@"%@", [[self.tagsArray objectAtIndex:indexPath.section] objectForKey:@"name"]];
        reuseView.backgroundColor = [UIColor lightGrayColor];
        return reuseView;
    } else {
        MenuHeaderCollectionViewCell *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        reuseView.nameLabel.text = @"";
        return reuseView;
    }
}

// collectionView点击的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuNextViewController *menuSearchVC = [[MenuNextViewController alloc] init];
    menuSearchVC.sName = [[[[self.tagsArray objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item] objectForKey:@"name"];
    [self.navigationController pushViewController:menuSearchVC animated:YES];
    [menuSearchVC release];
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
